package org.ysling.litemall.core.utils.response;
/**
 *  Copyright (c) [ysling] [927069313@qq.com]
 *  [litemall-plus] is licensed under Mulan PSL v2.
 *  You can use this software according to the terms and conditions of the Mulan PSL v2.
 *  You may obtain a copy of Mulan PSL v2 at:
 *              http://license.coscl.org.cn/MulanPSL2
 *  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 *  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 *  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *  See the Mulan PSL v2 for more details.
 */
import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageInfo;
import lombok.Getter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 响应操作结果
 * <pre>
 *  {
 *      errno： 错误码，
 *      errmsg：错误消息，
 *      data：  响应数据
 *  }
 * </pre>
 *
 * <p>
 * 错误码：
 * <ul>
 * <li> 0，成功；
 * <li> 4xx，前端错误，说明前端开发者需要重新了解后端接口使用规范：
 * <ul>
 * <li> 401，参数错误，即前端没有传递后端需要的参数；
 * <li> 402，参数值错误，即前端传递的参数值不符合后端接收范围。
 * </ul>
 * <li> 5xx，后端错误，除501外，说明后端开发者应该继续优化代码，尽量避免返回后端错误码：
 * <ul>
 * <li> 501，验证失败，即后端要求用户登录；
 * <li> 502，系统内部错误，即没有合适命名的后端内部错误；
 * <li> 503，业务不支持，即后端虽然定义了接口，但是还没有实现功能；
 * <li> 504，网络繁忙，请刷新重试，即后端采用了乐观锁更新，而并发更新时存在数据更新失效；
 * <li> 505，更新数据失败，即后端数据库更新失败（正常情况应该更新成功）。
 * </ul>
 * <li> 6xx，小商城后端业务错误码，
 * 具体见litemall-admin-api模块的AdminResponseCode。
 * <li> 7xx，管理后台后端业务错误码，
 * 具体见litemall-wx-api模块的WxResponseCode。
 * </ul>
 * @author Ysling
 */
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseUtil {

    /** 状态码 */
    private final String errno;
    /** 返回信息 */
    private final String errmsg;
    /** 返回实体结果 */
    private final Object data;

    public ResponseUtil(String errno, String errmsg, Object data) {
        this.errno = errno;
        this.errmsg = errmsg;
        this.data = data;
    }

    public static Object ok() {
        return new ResponseUtil(ResponseStatus.SUCCESS.getErrno(), ResponseStatus.SUCCESS.getErrMsg(), null);
    }

    public static Object ok(Object data) {
        return new ResponseUtil(ResponseStatus.SUCCESS.getErrno(), ResponseStatus.SUCCESS.getErrMsg(), data);
    }

    public static Object okList(List<?> list) {
        Map<String, Object> data = new HashMap<>();
        PageInfo<?> page = PageInfo.of(list);
        data.put("list", page.getList());
        data.put("total", page.getTotal());
        data.put("page", page.getPageNum());
        data.put("limit", page.getPageSize());
        data.put("pages", page.getPages());
        return new ResponseUtil(ResponseStatus.SUCCESS.getErrno(), ResponseStatus.SUCCESS.getErrMsg(), data);
    }

    public static Object okList(List<?> list, List<?> pageList) {
        Map<String, Object> data = new HashMap<>();
        PageInfo<?> page = PageInfo.of(pageList);
        data.put("list", list);
        data.put("total", page.getTotal());
        data.put("page", page.getPageNum());
        data.put("limit", page.getPageSize());
        data.put("pages", page.getPages());
        return new ResponseUtil(ResponseStatus.SUCCESS.getErrno(), ResponseStatus.SUCCESS.getErrMsg() , data);
    }

    public static Object fail() {
        return new ResponseUtil(ResponseStatus.FAILED.getErrno(), ResponseStatus.FAILED.getErrMsg(), null);
    }

    public static Object fail(String errmsg) {
        return new ResponseUtil(ResponseStatus.FAILED.getErrno(), errmsg, null);
    }

    public static Object fail(ResponseStatus status) {
        return new ResponseUtil(status.getErrno(), status.getErrMsg(), null);
    }

    public static Object fail(Integer errno, String errmsg) {
        return new ResponseUtil(String.valueOf(errno), errmsg, null);
    }

    public static Object fail(String errno, String errmsg) {
        return new ResponseUtil(errno, errmsg, null);
    }

    public static Object fail(String errno, String errmsg, Object data) {
        return new ResponseUtil(errno, errmsg, data);
    }

    /**参数不对*/
    public static Object badArgument() {
        return fail(ResponseStatus.USER_ERROR_A0400);
    }

    /**参数值不对*/
    public static Object badArgumentValue() {
        return fail(ResponseStatus.USER_ERROR_A0410);
    }

    /**请登录*/
    public static Object unlogin() {
        return fail(ResponseStatus.USER_ERROR_A0223);
    }

    /**系统内部错误*/
    public static Object serious() {
        return fail(ResponseStatus.SYSTEM_ERROR_B0001);
    }

    /**网络繁忙，请刷新重试*/
    public static Object updatedDateExpired() {
        return fail(ResponseStatus.SYSTEM_ERROR_B0000);
    }

    /**更新数据失败*/
    public static Object updatedDataFailed() {
        return fail(ResponseStatus.SYSTEM_ERROR_B0000);
    }

}

